<?php
declare (strict_types = 1);

namespace app;

use app\model\ArticleModel;
use app\model\ClassifyModel;
use app\model\ConfigModel;
use think\App;
use think\exception\ValidateException;
use think\Validate;
use think\facade\View;
/**
 * 控制器基础类
 */
abstract class BaseController
{
    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function __construct(App $app)
    {
        $this->app     = $app;
        $this->request = $this->app->request;

        // 控制器初始化
        $this->initialize();
    }

    // 初始化
    protected function initialize()
    {

        $catid = input('catid', 0);
        View::assign('catid', $catid);

        $classify = ClassifyModel::where(['deleted' => 0, 'id'=>$catid])->find();
        View::assign('classify', $classify);

        // 获取网站配置 根据group 分组
        $config = ConfigModel::field('id,name,title,content,type')->where(['deleted' => 0,'tenant_id' => 1])->select();
        foreach ($config as $key => $value) {
            if ($value['type'] == 'images') {
                $value['content'] = explode(',', $value['content']);
            }
        }
        View::assign('config', $config);
        // 获取网站配置 SEO信息 $config中 web_site_title web_site_keyword web_site_description
        $seo = [];

        foreach ($config as $key => $value) {
            if ($value['name'] == 'web_site_title') {
                $seo['title'] = $value['content'];
            }
            if ($value['name'] == 'web_site_keyword') {
                $seo['keyword'] = $value['content'];
            }
            if ($value['name'] == 'web_site_description') {
                $seo['description'] = $value['content'];
            }
        }
        View::assign('SEO', $seo);

    }

    /**
     * 验证数据
     * @access protected
     * @param  array        $data     数据
     * @param  string|array $validate 验证器名或者验证规则数组
     * @param  array        $message  提示信息
     * @param  bool         $batch    是否批量验证
     * @return array|string|true
     * @throws ValidateException
     */
    protected function validate(array $data, $validate, array $message = [], bool $batch = false)
    {
        if (is_array($validate)) {
            $v = new Validate();
            $v->rule($validate);
        } else {
            if (strpos($validate, '.')) {
                // 支持场景
                [$validate, $scene] = explode('.', $validate);
            }
            $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
            $v     = new $class();
            if (!empty($scene)) {
                $v->scene($scene);
            }
        }

        $v->message($message);

        // 是否批量验证
        if ($batch || $this->batchValidate) {
            $v->batch(true);
        }

        return $v->failException(true)->check($data);
    }


    /**
     * 通用方法 获取资讯分页列表
     */
    protected function getArticleList($catid) {
        $where = [
            'deleted' => 0,
            'status_flag' => 0,
            'catid' => $catid
        ];



        $list = ArticleModel::where($where)->order( "show_time desc")->paginate(10);

        // 把分页数据赋值给模板变量list
        View::assign('list', $list);

        // 获取分页显示
        $page = $list->render();
        // 分页数据
        View::assign('page', $page);
    }

    /**
     * 根据分类获取推荐资讯
     */
    protected function getHotList($catid)
    {
        $where = [
            'deleted' => 0,
            'status_flag' => 0,
            'catid' => $catid
        ];
        $hot_list = ArticleModel::where($where)->order('hits desc')->limit(10)->select();
        View::assign('hot_list', $hot_list);
    }

}
